package controllers

import (
	"beego_background_framework/models"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
)

//管理员权限
type AdminRuleController struct {
	BaseController
}

func (c *AdminRuleController) Index(){
	c.Data["treeMenus"] = c.GetRuleList()
	c.SetTpl("admin_rule/index.html")
}

func (c *AdminRuleController) Create()  {
	//一级菜单
	c.Data["ruleList"], _ = models.AdminAuthRuleGetRootRule()
	c.SetTpl("admin_rule/create.html")
}

func (c *AdminRuleController) Add()  {
	var adminAuthRule models.AdminAuthRule
	_ = c.Ctx.Input.Bind(&adminAuthRule.Type, "type")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Pid, "pid")
	_ = c.Ctx.Input.Bind(&adminAuthRule.SkipUrl, "skip_url")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Name, "name")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Icon, "icon")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Remark, "remark")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Weigh, "weigh")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Status, "status")

	// 验证字段
	valid := validation.Validation{}
	valid.Required(adminAuthRule.Name,"name").Message("标题不能为空")
	if valid.HasErrors(){
		for _,err := range valid.Errors{
			c.ApiError(err.Message,nil)
		}
	}
	o := orm.NewOrm()
	_, err := o.Insert(&adminAuthRule)
	if err != nil{
		_ = o.Rollback()
		c.ApiError(err.Error(),nil)
	}
	c.ApiSuccess("添加成功",nil)
}

func (c *AdminRuleController) Edit()  {
	id, err := c.GetUint64(":id")
	if err != nil{
		c.ApiError("数据类型有误",nil)
	}
	o := orm.NewOrm()
	adminRuthRule := models.AdminAuthRule{Id:uint(id)}
	err = o.Read(&adminRuthRule)
	if err == orm.ErrNoRows{
		c.ApiError("未找到信息",nil)
	}

	c.Data["info"] = adminRuthRule
	c.Data["ruleList"], _ = models.AdminAuthRuleGetRootRule()
	c.SetTpl("admin_rule/edit.html")
}

func (c *AdminRuleController) Update()  {
	var adminAuthRule models.AdminAuthRule
	o := orm.NewOrm()
	_ = c.Ctx.Input.Bind(&adminAuthRule.Id, ":id")
	err := o.Read(&adminAuthRule)
	if err != nil{
		c.ApiError("查询失败",nil)
	}
	_ = c.Ctx.Input.Bind(&adminAuthRule.Type, "type")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Pid, "pid")
	_ = c.Ctx.Input.Bind(&adminAuthRule.SkipUrl, "skip_url")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Name, "name")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Icon, "icon")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Remark, "remark")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Weigh, "weigh")
	_ = c.Ctx.Input.Bind(&adminAuthRule.Status, "status")

	// 验证字段
	valid := validation.Validation{}
	valid.Required(adminAuthRule.Name,"name").Message("标题不能为空")
	if valid.HasErrors(){
		for _,err := range valid.Errors{
			c.ApiError(err.Message,nil)
		}
	}
	_, err = o.Update(&adminAuthRule)
	if err != nil{
		c.ApiError(err.Error(),nil)
	}

	c.ApiSuccess("操作成功",nil)
}

func (c *AdminRuleController) Destroy(){
	id, _ := c.GetUint64(":id")
	o := orm.NewOrm()

	//检查是否还有子节点
	cnt, _ := o.QueryTable(new(models.AdminAuthRule)).Filter("Pid", id).Count()
	if cnt > 0{
		c.ApiError("无法删除，存在子节点",nil)
	}
	if num,err :=o.Delete(&models.AdminAuthRule{Id: uint(id)});err == nil{
		if num > 0{
			c.ApiSuccess("操作成功",nil)
		}
	}
	c.ApiError("操作失败",nil)
}

// 设置权限状态
func (c *AdminRuleController) UpdateStatus(){
	id, _ := c.GetUint64("id")
	status, _ := c.GetUint8("status")
	adminAuthRule := models.AdminAuthRule{Id:uint(id)}

	o := orm.NewOrm()
	if o.Read(&adminAuthRule) == nil{
		//设置会员状态
		adminAuthRule.Status = status
		if num, err := o.Update(&adminAuthRule); err == nil{
			c.ApiSuccess("操作成功", map[string]interface{}{"status":adminAuthRule.Status,"num":num})
		}
	}
	c.ApiError("操作失败",nil)
}

//icon列表
func (c *AdminRuleController) Icon(){
	c.SetTpl("admin_rule/icon.html")
}

